<?php
/**
 * golangshop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 上海瑞璞玉数字网络科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.golangshop.com
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */

namespace addon\cashier\storeapi\controller;

use addon\coupon\model\Coupon as CouponModel;
use app\storeapi\controller\BaseStoreApi;

class Coupon extends BaseStoreApi
{
    /**
     * 列表
     */
    public function lists()
    {
        $page = $this->params[ 'page' ] ?? 1;
        $page_size = $this->params[ 'page_size' ] ?? PAGE_LIST_ROWS;
        $name = $this->params[ 'name' ] ?? '';
        $start_time = $this->params[ 'fetch_time_start' ] ?? '';
        $end_time = $this->params[ 'fetch_time_end' ] ?? '';
        $type = $this->params[ 'type' ] ?? '';
        $use_type = $this->params[ 'use_type' ] ?? '';
        $coupon_model = new CouponModel();
        $condition = [
            [ 'site_id', "=", $this->site_id ],
        ];
        if (!empty($type)) {
            $condition[] = [ "type", '=', $type ];
        }
        if (!empty($use_type)) {
            $condition[] = [ "use_type", '=', $use_type ];
        }
        if ($name !== '') {
            $condition[] = [ "name", 'like', '%' . $name . '%' ];
        }
        if (!empty($start_time) && empty($end_time)) {
            $condition[] = [ 'create_time', '>=', date_to_time($start_time) ];
        } elseif (empty($start_time) && !empty($end_time)) {
            $condition[] = [ "create_time", "<=", date_to_time($end_time) ];
        } elseif (!empty($start_time) && !empty($end_time)) {
            $condition[] = [ 'create_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
        }
        $list = $coupon_model->getCouponPageList($condition, $page, $page_size, 'coupon_id desc');
        return $this->response($list);
    }

    /**
     * 优惠券详情
     */
    public function detail()
    {
        $coupon_id = $this->params[ 'coupon_id' ] ?? 0;
        $coupon_model = new CouponModel();
        $condition = array(
            ['site_id', '=', $this->site_id],
            ['coupon_id', '=', $coupon_id]
        );
        $info = $coupon_model->getCouponInfo($condition,'*');
        return $this->response($info);

    }

    /**
     * 查询优惠券
     * @return false|string
     */
    public function couponTypeList()
    {
        $page = $this->params[ 'page' ] ?? 1;
        $page_size = $this->params[ 'page_size' ] ?? PAGE_LIST_ROWS;
        $name = $this->params[ 'name' ] ?? '';
        $coupon_model = new CouponModel();
        $condition = [
            [ 'site_id', "=", $this->site_id ],
            [ 'status', '=', 1]
        ];
        if ($name !== '') {
            $condition[] = [ "coupon_name", 'like', '%' . $name . '%' ];
        }
        $field = 'coupon_type_id,type,coupon_name,count,lead_count,used_count,goods_type,is_limit,at_least,money,discount,discount_limit,validity_type,end_time,fixed_term';
        $list = $coupon_model->getCouponTypePageList($condition, $page, $page_size, 'coupon_type_id desc', $field);
        return $this->response($list);
    }
}